package com.tbs.parkingsaleapp.Http.Listenner;

import android.content.Context;
import com.alibaba.fastjson.JSON;
import com.tbs.parkingsaleapp.Http.HttpHelper;
import com.tbs.parkingsaleapp.Http.IAsycHttpBackgroundAction;
import com.tbs.parkingsaleapp.Http.IAsycHttpResultAction;
import com.tbs.parkingsaleapp.Http.JsonData.BasicOrder;
import com.tbs.parkingsaleapp.Http.JsonData.DiscountInfo;
import com.tbs.parkingsaleapp.SystemManager.NetWorkSetting;
import com.tbs.parkingsaleapp.UI.DiscountApplyActivity;
import com.tbs.parkingsaleapp.UI.OrderPage.OrderPageFragment;

import java.util.ArrayList;

public class UseDiscountHttper implements IAsycHttpBackgroundAction<Integer, BasicOrder>, IAsycHttpResultAction<BasicOrder> {

    Context context;
    int order,apply;
    ArrayList<Integer> data=new ArrayList<>();
    public void setDiscount(Context context, ArrayList<DiscountInfo> is)
    {
        data.clear();
        for (DiscountInfo i:is
             ) {
            data.add(i.getDiscount().getId());
        }
    }

    @Override
    public BasicOrder BackGroundAction(Integer... params) throws Exception {

        order=params[0];apply=params[1];
        String d=JSON.toJSONString(data);
        String r= HttpHelper.Post(d, NetWorkSetting.useDiscountsUrl(params[0],params[1]));
        return JSON.parseObject(r,BasicOrder.class);
    }

    @Override
    public void ResultFunction(BasicOrder result) {
        if(apply==0)
        {
            DiscountApplyActivity.getInstance().setNEWPrice(result.getPrice());
            DiscountApplyActivity.getInstance().btn.setEnabled(true);
        }
        else
        {
            OrderPageFragment.UpdateList(context);
            DiscountApplyActivity.getInstance().finish();
        }

    }
}
